home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / inherit7.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  3KB  |  138 lines

  1.                                   // Chapter 8 - Program 7
  2. #include <iostream.h>
  3.  
  4. class vehicle {
  5. protected:
  6.    int wheels;
  7.    float weight;
  8. public:
  9.    void initialize(int in_wheels, float in_weight);
  10.    int get_wheels(void) {return wheels;}
  11.    float get_weight(void) {return weight;}
  12.    float wheel_loading(void) {return weight/wheels;}
  13. };
  14.  
  15.  
  16.  
  17.  
  18. class car : public vehicle {
  19.    int passenger_load;
  20. public:
  21.    void initialize(int in_wheels, float in_weight, int people = 4);
  22.    int passengers(void) {return passenger_load;}
  23. };
  24.  
  25.  
  26.  
  27.  
  28. class truck : public vehicle {
  29.    int passenger_load;
  30.    float payload;
  31. public:
  32.    void init_truck(int how_many = 2, float max_load = 24000.0);
  33.    float efficiency(void);
  34.    int passengers(void) {return passenger_load;}
  35. };
  36.  
  37.  
  38.  
  39.  
  40. main()
  41. {
  42. vehicle unicycle;
  43.  
  44.    unicycle.initialize(1, 12.5);
  45.    cout << "The unicycle has " <<
  46.                                 unicycle.get_wheels() << " wheel.\n";
  47.    cout << "The unicycle's wheel loading is " <<
  48.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  49.    cout << "The unicycle weighs " <<
  50.                              unicycle.get_weight() << " pounds.\n\n";
  51.  
  52. car sedan[3];
  53. int index;
  54.  
  55.    for (index = 0 ; index < 3 ; index++) {
  56.       sedan[index].initialize(4, 3500.0, 5);
  57.       cout << "The sedan carries " << sedan[index].passengers() <<
  58.                                                     " passengers.\n";
  59.       cout << "The sedan weighs " << sedan[index].get_weight() <<
  60.                                                         " pounds.\n";
  61.       cout << "The sedan's wheel loading is " <<
  62.              sedan[index].wheel_loading() << " pounds per tire.\n\n";
  63.    }
  64.  
  65. truck *semi;
  66.  
  67.    semi = new truck;
  68.    semi->initialize(18, 12500.0);
  69.    semi->init_truck(1, 33675.0);
  70.    cout << "The semi weighs " << semi->get_weight() << " pounds.\n";
  71.    cout << "The semi's efficiency is " <<
  72.                           100.0 * semi->efficiency() << " percent.\n";
  73.    delete semi;
  74. }
  75.  
  76.  
  77.  
  78.  
  79.               // initialize to any data desired
  80. void
  81. vehicle::initialize(int in_wheels, float in_weight)
  82. {
  83.    wheels = in_wheels;
  84.    weight = in_weight;
  85. }
  86.  
  87.  
  88.  
  89. void
  90. car::initialize(int in_wheels, float in_weight, int people)
  91. {
  92.    passenger_load = people;
  93.    wheels = in_wheels;
  94.    weight = in_weight;
  95. }
  96.  
  97.  
  98.  
  99.  
  100. void
  101. truck::init_truck(int how_many, float max_load)
  102. {
  103.    passenger_load = how_many;
  104.    payload = max_load;
  105. }
  106.  
  107.  
  108.  
  109. float
  110. truck::efficiency(void)
  111. {
  112.    return payload / (payload + weight);
  113. }
  114.  
  115.  
  116.  
  117.  
  118. // Result of execution
  119. //
  120. // The unicycle has 1 wheel.
  121. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  122. // The unicycle weighs 12.5 pounds.
  123. //
  124. // The sedan carries 5 passengers.
  125. // The sedan weighs 3500 pounds.
  126. // The sedan's wheel loading is 875 pounds per tire.
  127. //
  128. // The sedan carries 5 passengers.
  129. // The sedan weighs 3500 pounds.
  130. // The sedan's wheel loading is 875 pounds per tire.
  131. //
  132. // The sedan carries 5 passengers.
  133. // The sedan weighs 3500 pounds.
  134. // The sedan's wheel loading is 875 pounds per tire.
  135. //
  136. // The semi weighs 12500 pounds.
  137. // The semi's efficiency is 72.929072 percent.
  138.